﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Courses : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {

        }
        else
        {
            Setup();
        }
    }

    private void Setup()
    {
        Course.REVIEW_FOLDER = Page.Server.MapPath("App_Data") + "\\reviews"; // "~\\App_Settings"; 
        FillTable();
    }

    private void FillTable()
    {
        Course course = new Course();
        List<Course> reviews = course.GetAllReviews();
        reviews.Add(new Course("title", "descr", "content", "rate", "file"));

        foreach (Course c in reviews)
        {
            TableRow row = new TableRow();
            TableCell cell = new TableCell();
            cell.Controls.Add(new LiteralControl("<hr />"));
            row.Cells.Add(cell);
            tblReviews.Rows.Add(row);
            AddReview(c);
        }
            //AddTableRow(c);
    }

    
    private void AddReview(Course c)
    {
        TableRow row = new TableRow();
        TableCell cell = new TableCell();
        cell.Controls.Add(new LiteralControl(c.Title));
        row.Cells.Add(cell);
        tblReviews.Rows.Add(row);

        row = new TableRow();
        cell = new TableCell();
        cell.Controls.Add(new LiteralControl(c.Description));
        row.Cells.Add(cell);
        tblReviews.Rows.Add(row);

        row = new TableRow();
        cell = new TableCell();
        cell.Controls.Add(new LiteralControl(c.Rating));
        row.Cells.Add(cell);
        tblReviews.Rows.Add(row);

        row = new TableRow();
        cell = new TableCell();
        cell.Controls.Add(new LiteralControl(c.Content));
        row.Cells.Add(cell);
        tblReviews.Rows.Add(row);

        row = new TableRow();
        cell = new TableCell();
        cell.Controls.Add(new LiteralControl("<hr />"));
        row.Cells.Add(cell);
        tblReviews.Rows.Add(row);
    }



    protected void btnAddCourse_Click(object sender, EventArgs e)
    {
        Course course = new Course(txtTitle.Text, txtDescription.Text, txtContent.Text,
                                         lstRating.SelectedValue, Course.REVIEW_FOLDER + "\\" + txtTitle.Text + ".txt");
        course.CreateReview();
        FillTable();
    }


    /*
private void AddTableRow(Course c)
{
    TableRow row = new TableRow();

    TableCell cell = new TableCell();
    cell.Controls.Add(new LiteralControl(c.Title));
    row.Cells.Add(cell);

    cell = new TableCell();
    cell.Controls.Add(new LiteralControl(c.Description));
    row.Cells.Add(cell);

    cell = new TableCell();
    cell.Controls.Add(new LiteralControl(c.Rating));
    row.Cells.Add(cell);

    cell = new TableCell();
    cell.Controls.Add(new LiteralControl(c.Content));
    row.Cells.Add(cell);

    cell = new TableCell();
    //string item = string.Format("<a href='{0}{1}'/>", c.ReviewFolder, c.ReviewFile);
    // string item = string.Format("<a href='{0}{1}'/>", Course.REVIEW_FOLDER, c.ReviewFile);
    // string item = string.Format("<a href='{0}{1}'></a>", Course.REVIEW_FOLDER, c.ReviewFile);
    //string item = string.Format("<a href='www.google.com'></a>", Course.REVIEW_FOLDER, c.ReviewFile);
    //string item = string.Format("{0}{1}", Course.REVIEW_FOLDER, c.ReviewFile);
    LinkButton link = new LinkButton();
    link.PostBackUrl = c.ReviewFile;
    // link.ID = item;
    link.Text = Path.GetFileName(c.ReviewFile);
    cell.Controls.Add(link);
        
    row.Cells.Add(cell);

    tblReviews.Rows.Add(row);
}
 * */

}